﻿//****************************************************************************
// Copyright (c) Microsoft Corporation. All rights reserved.
//****************************************************************************
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.FxCop.Sdk;

namespace Microsoft.FxCop.Rules.ASP.Security
{
    class EnableEventValidationShouldBeTrue : BasePageIntrospectionRule
    {

        #region *** Constructor ***
        public EnableEventValidationShouldBeTrue()
            : base("EnableEventValidationShouldBeTrue")
        {
        }
        #endregion

        #region *** Methods ***

        #region *** BasePageRule ***
        protected override void CheckBuildControlTree(Method buildTreeMethod)
        {
            AnalyzeMethodForEnableEventValidationToFalse(buildTreeMethod);
        }

        private void AnalyzeMethodForEnableEventValidationToFalse(Method method)
        {
            Instruction currentInstruction;
            Instruction peekInstruction;

            for (int i = 0; i < method.Instructions.Count; i++)
            {
                currentInstruction = method.Instructions[i];

                if (currentInstruction.OpCode == OpCode.Callvirt)
                {
                    if ((currentInstruction.Value as Method).Name.Name == "set_EnableEventValidation")
                    {
                        peekInstruction = method.Instructions[i - 1];
                        if (peekInstruction.OpCode == OpCode.Ldc_I4_0)
                        {
                            ReportProblem();
                        }

                        // we found the Set_EnableVentValidation. This will only occur once so we can return
                        return;
                    }
                }
            }
        }
        #endregion

        #endregion
    }
}
